const app = getApp()
import {
  imageUrl
} from "../../utils/imageutil.js"
Page({
  data: {
    imageUrl,
    left_icon: 'back_icon',
    navcurr: 1,
    keys: '',
    page: 1,
    flag: true,
    type: '',
    navlist: [{
      title: '全部',
      id: 1
    }, {
      title: '待付款',
      id: 2
    }, {
      title: '待发货',
      id: 3
    }, {
      title: '待收货',
      id: 4
    }, {
      title: '已完成',
      id: 5
    }],
    list: [],
    is_refund: false,
    nowTime: 0,
  },
  changernavcurr(e) { //订单顶部状态切换
    this.setData({
      navcurr: e.currentTarget.dataset.id,
      type: e.currentTarget.dataset.id - 1,
      page: 1,
      flag: true,
      list: []
    })
    this.getList()
  },
  change_keys(e) {
    this.setData({
      keys: e.detail.value,
      flag: true,
      page: 1,
      list: []
    })
    this.getList()
  },
  onLoad: function (options) {
    if (options.navcurr) {
      this.setData({
        navcurr: options.navcurr,
        type: Number(options.navcurr) - 1
      })
    }
    if (getCurrentPages().length == 1) {
      this.setData({
        left_icon: "w_home"
      })
    }
    this.setData({
      nowTime: new Date().getTime()
    })
  },
  onShow() {
    console.log(1)
    this.setData({

      flag: true,
      page: 1,
      list: []
    })
    this.getList()
  },
  countDown(endTime, index) {
    return new Promise((a, b) => {
      let times = setInterval(() => {
        const nowTime = new Date().getTime();
        let distance = endTime - nowTime;
        if (distance < 0) {
          clearInterval(times)
          this.setData({
            ['list[' + index + '].countDown']: ''
          })
          a('')
        } else {
          const days = Math.floor(distance / (1000 * 60 * 60 * 24));
          const hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
          const minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
          const seconds = Math.floor((distance % (1000 * 60)) / 1000);

          this.setData({
            ['list[' + index + '].countDown']: this.formatTime(minutes) + ':' + this.formatTime(seconds)
          })
          a(this.formatTime(minutes) + ':' + this.formatTime(seconds))
        }
      }, 1000);
    })

  },

  formatTime(time) {
    return time < 10 ? '0' + time : time;
  },
  stop_tap() {},
  to_shopInfo(e) {
    console.log(e, '45');
    wx.navigateTo({
      url: '/store/info?id=' + e.currentTarget.dataset.id,
    })
    console.log('头像');
  },
  onReachBottom() {
    this.getList()
  },
  applyRefundJfOrder(e) {
    if (e.currentTarget.dataset.refund > 0) {
      return
    }
    wx.navigateTo({
      url: '/pages/order/returnGoods?order=' + e.currentTarget.dataset.order,
    })
    return
    getApp().globalData.posts('/api/ouyoung/order/applyRefundJfOrder', {
      order_number: e.currentTarget.dataset.order
    }).then(res => {
      getApp().showModal(res.data.msg).then(() => {
        this.setData({
          page: 1,
          list: []
        })
        this.getList()
      })
    })
  },
  to_Cancel(e) { //取消订单
    wx.navigateTo({
      url: '../matron/abrogate?wid=' + e.currentTarget.dataset.id + '&type=' + 3,
    })
  },
  jfcheck(e) { //确认收货
    app.globalData.post({
      data: {
        do: 'jfcheck',
        jforder_id: e.currentTarget.dataset.id,
        user_id: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: "none"
      })
      if (res.data.code == 1) {
        this.setData({
          page: 1,
          list: []
        })
        this.getList()
      }
    })
  },
  pays(e) {
    if (e.currentTarget.dataset.id.indexOf(",") != -1) {
      wx.navigateTo({
        url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid,
      })
    } else {
      // this.data.orderDetail.num >= 2 &&
      if (e.currentTarget.dataset.cartid) {
        wx.navigateTo({
          url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid,
        })

      } else {
        if (e.currentTarget.dataset.ggs) {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id,
          })
        } else {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id,
          })
        }
      }
    }
  },
  pay(e) { //微信支付
    if (e.currentTarget.dataset.id.indexOf(",") != -1) {
      wx.navigateTo({
        url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid + '&orderid=' + e.currentTarget.dataset.orderid,
      })
    } else {
      if (this.data.list[e.currentTarget.dataset.index].num >= 2) {
        wx.navigateTo({
          url: '/store/submit_more?ids=' + e.currentTarget.dataset.cartid + '&orderid=' + e.currentTarget.dataset.orderid,
        })
      } else {
        if (e.currentTarget.dataset.ggs) {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id + '&ggs=' + e.currentTarget.dataset.ggs + '&orderid=' + e.currentTarget.dataset.orderid,
          })
        } else {
          wx.navigateTo({
            url: '/store/submit?id=' + e.currentTarget.dataset.id + '&orderid=' + e.currentTarget.dataset.orderid,
          })
        }
      }
    }
    // let _this = this;
    // app.globalData.post({
    //   data:{
    //     do:'jfpay',
    //     order_id:e.currentTarget.dataset.id,
    //     user_id:wx.getStorageSync('userInfo').id,
    //     openid:wx.getStorageSync('userInfo').openid,
    //     type:2
    //   }
    // }).then(res=>{
    //   console.log(res)
    //   let data = res.data
    //   wx.requestPayment({
    //     nonceStr: data.nonceStr,
    //     package: data.package,
    //     paySign: data.paySign,
    //     timeStamp: data.timeStamp,
    //     signType: data.signType,
    //     complete(s){
    //       console.log(s)
    //       if ("requestPayment:ok" == s.errMsg) {
    //         wx.showToast({
    //           title: "支付成功"
    //         })
    //         _this.setData({
    //           navcurr:1,
    //           list:[],
    //         })
    //         _this.getList()
    //       }
    //     }
    //   })  
    // })
  },
  to_pay2(e) {
    wx.navigateTo({
      url: '../../order/order_payment?id=' + e.currentTarget.dataset.id + '&ordertype=' + e.currentTarget.dataset.ordertype,
    })
    // let _this = this;
    // app.globalData.posts("/index.php/api/ouyoung/chat/sorting/ouyapi/enjoy_pay",{
    //  id:e.currentTarget.dataset.id,
    //  actual_integral:e.currentTarget.dataset.actual_integral,

    // }).then(res=>{
    //   let data = res.data.data
    //   console.log(data.nonceStr,'data.nonceStr');
    //   wx.requestPayment({
    //     nonceStr: data.nonceStr,
    //     package: data.package,
    //     paySign: data.paySign,
    //     timeStamp: data.timeStamp,
    //     signType: data.signType,
    //     complete(s){
    //       console.log(s)
    //       if ("requestPayment:ok" == s.errMsg) {
    //         wx.showToast({
    //           title: "支付成功"
    //         })
    //         _this.setData({
    //           navcurr:1,
    //           list:[],
    //         })
    //         _this.getList()
    //       }
    //     }
    //   })  
    // })
  },
  getList() { //订单列表
    if (!this.data.flag) {
      return
    }
    this.setData({
      flag: false

    })
    app.globalData.post({
      data: {
        do: 'Dhmx',
        type: this.data.type,
        key: this.data.keys,
        page: this.data.page,
        pagesize: 10,
        user_id: wx.getStorageSync('userInfo').id
      }
    }).then(res => {
      wx.stopPullDownRefresh();
      if (res.data.data == '' || res.data.data.length == 0) {
        this.setData({
          flag: false
        })
      } else {
        let data = res.data.data
        data.forEach((v, i) => {
          if (v.clustering == 1 && v.state == 2) {
            this.countDown((v.update_time * 1 + 30 * 60) * 1000, i)
          }
        })
        let arr = this.data.list.concat(data);
        this.setData({
          list: arr,
          flag: true,
          page: this.data.page + 1
        })
      }
    })
  },

  get_refundJforder(e) {
    let order_number = e.currentTarget.dataset.order_id
    getApp().showModal('是否取消拼团', true).then(() => {
      getApp().globalData.posts('/index.php/api/ouyoung/order/refundJforder', {
        order_number,
        tg: 1
      }).then(res => {
        getApp().showModal(res.data.msg).then(() => {
          if (res.data.code == 1) {
            this.setData({
              keys: '',
              flag: true,
              page: 1,
              list: []
            })
            this.getList()
          }
        })
      })
    })
  },

  get_pt(e) {
    let clustering = e.currentTarget.dataset.clustering
  },

  onShareAppMessage(e) {
    console.log(e)
    let from = e.from
    let url = ''

    if (from == 'menu') {
      url = encodeURIComponent('/pages/index/index')
      return {
        path: '/pages/index/blank?to_id=' + wx.getStorageSync('userInfo').id + '&tourl=' + url,
        title: this.data.title_name,
      }
    } else {
      let id = e.target.dataset.id
      let ordertype = e.target.dataset.ordertype
      let good_name = e.target.dataset.good_name
      let good_img = e.target.dataset.good_img
      let good_id = e.target.dataset.good_id
      let user_id = e.target.dataset.user_id
      url = encodeURIComponent('/store/submit?id=' + good_id + '&heqiong=0&jfrecord_id=' + id + '&tz_id=' + user_id)
      return {
        path: '/pages/index/blank?to_id=' + wx.getStorageSync('userInfo').id + '&tourl=' + url,
        title: good_name,
        imageUrl: good_img
      }
    }
  },


  to_particulars(e) { //订单详情
    wx.navigateTo({
      url: './particulars?id=' + e.currentTarget.dataset.id + '&ordertype=' + e.currentTarget.dataset.ordertype,
    })
  },
  appraise(e) { //评价
    wx.navigateTo({
      url: './mall_appraise?id=' + e.currentTarget.dataset.id + '&ordertype=' + e.currentTarget.dataset.ordertype,
    })
  },
  catch_tap(){},
})